package org.jetlinks.pro.koudai.web;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.api.crud.entity.QueryOperation;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.annotation.QueryAction;
import org.hswebframework.web.authorization.annotation.Resource;
import org.jetlinks.pro.koudai.Services.CourseService;
import org.jetlinks.pro.koudai.entity.CourseEntity;
import org.jetlinks.pro.koudai.entity.CourseInfo;
import org.jetlinks.pro.koudai.tenant.KoudaiAssetType;
import org.jetlinks.pro.tenant.AssetType;
import org.jetlinks.pro.tenant.crud.TenantCorrelatesAccessCrudController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import javax.annotation.Nonnull;
import java.util.function.Function;

@RestController
@RequestMapping("/course")
@Resource(id = "course-manager", name = "课程管理")
@Tag(name = "课程管理")
public class CourseController implements TenantCorrelatesAccessCrudController<CourseEntity, String> {

    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }


    @Override
    public CourseService getService() {
        return courseService;
    }

    @Nonnull
    @Override
    public AssetType getAssetType() {
        return KoudaiAssetType.course;
    }

    @Nonnull
    @Override
    public Function<CourseEntity, ?> getAssetIdMapper() {
        return CourseEntity::getId;
    }

    @Nonnull
    @Override
    public String getAssetProperty() {
        return "course";
    }

    //获取详情
    @GetMapping("/{id:.+}/detail")
    @QueryAction
    @Operation(summary = "获取指定ID课程详情")
    public Mono<CourseInfo> getCourseDetailInfo(@PathVariable @Parameter(description = "课程ID") String id) {
        return courseService.getCourseInfo(id);
    }
    /**
     * 分页查询设备详情列表
     *
     * @param query 查询条件
     * @return 设备详情
     */
    @GetMapping("/detail/_query")
    @QueryAction
    @QueryOperation(summary = "分页动态查询课程详情")
    public Mono<PagerResult<CourseInfo>> queryDeviceDetailPager(@Parameter(hidden = true) QueryParamEntity query) {
        return courseService.queryCourseInfo(query);
    }
}
